<template>
  <div id="invitePlayer">
    <el-form ref="form" :model="form" label-width="80px" label-suffix = ":" >
      <el-form-item label="用户名">
        <el-input v-model="form.userName" style="width: 255px;margin-right: 80px" placeholder="请输入被邀请人用户名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit(form.userName)" :disabled=this.$store.state.createTeam_isShow>发送邀请</el-button>
        <el-button @click="onReset" :disabled=this.$store.state.createTeam_isShow>取消</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>

  import Axios from "axios";

  const instance = Axios.create({
    baseURL: 'http://localhost:8081/api/pri/team',
    timeout: 5000,
    headers: { "content-type": "application/json" }
  })

    export default {
      //import引入的组件需要注入到对象中才能使用",
      components: {},
      data() {
        //这里存放数据",
        return {
          form:{
            userName:''
          },
        };
      },
      //方法集合",
      methods: {
        onSubmit(userName){

          const Qs = require('qs');
          let judge = /^[0-9]+.?[0-9]*/;
          if (!judge.test(userId)){
            this.$message({
              message:'请输入正确的用户名!',
              type:'error'
            })
          }
          else{
            instance.post('finTeamByUserId',Qs.stringify({userName:userName}))
            .then(res=>{
              if (res.data != null){
                this.$message({
                  message:'该用户已有队伍!',
                  type:'error'
                })
              }
              else {
                instance.post('inviteJoinTeam',Qs.stringify({userName:userName,teamId:this.$store.state.all_teamId}))
                .then(res2=>{
                  if (res2.data.code === 0){
                    this.$message({
                      message:'邀请成功，队员已加入!',
                      type:'success'
                    })
                  }
                  else{
                    this.$message({
                      message:'邀请失败，请联系管理员!',
                      type:'error'
                    })
                  }

                })
              }
            })
          }


        },


        onReset(){
          this.form.userId='';
        }


      },
      //生命周期 - 创建完成（可以访问当前this实例）",数据模型已加载，方法已加载,html模板已加载,html模板未渲染
      created() {

      },
    }

</script>

<style>

</style>

